package com.nbgroup.utils;

import java.io.Serializable;
import java.util.List;


/**
 * @author 王玉涛
 * @Param
 * @date 2017-10-22 13:22
 */
public class PageUtil<T> implements Serializable {
    /**
     * content 每页的内容
     */
    private List<T> content;
    /**
     * 是否有上一页
     */
    private boolean first;
    /**
     * 是否有下一页
     */
    private boolean last;

    private Integer numberOfElements = 10;
    ;
    /**
     * number 当前页
     */
    private Integer number = 0;
    /**
     * size 每页大小
     */
    private Integer size = 10;
    /**
     * totalPages 总页数
     */
    private Integer totalPages;
    /**
     * totalElements 总条数
     */
    private Integer totalElements = 0;


    // 以下set方式是需要赋值的

    /**
     * 设置当前页 <br>
     *
     * @param number
     * @author kangxu
     */
    public void setNumber(Integer number) {
        this.number = number;
    }

    public Integer getNumberOfElements() {
        return numberOfElements;
    }

    public void setNumberOfElements(Integer numberOfElements) {
        this.numberOfElements = numberOfElements;
    }

    /**
     * 设置每页大小,也可以不用赋值,默认大小为10条 <br>
     *
     * @param size
     * @author kangxu
     */
    public void setSize(Integer size) {
        this.size = size;
    }

    /**
     * 设置总条数,默认为0 <br>
     *
     * @param totalElements
     * @author kangxu
     */
    public void setTotalElements(Integer totalElements) {
        this.totalElements = totalElements;
        otherAttr();
    }

    public Integer getTotalElements() {
        return totalElements;
    }

    /**
     * 设置分页内容 <br>
     *
     * @param content
     * @author kangxu
     */
    public void setContent(List<T> content) {
        this.content = content;
    }

    public boolean isFirst() {
        first = number == 0;
        return first;
    }

    public void setFirst(boolean first) {
        this.first = first;
    }

    public boolean isLast() {
        //判定边界
        last = number == totalPages - 1;
        return last;
    }

    public void setLast(boolean last) {
        this.last = last;
    }

    public void setTotalPages(Integer totalPages) {
        this.totalPages = totalPages;
    }

    /**
     * 设置其他参数
     *
     * @author kangxu
     */
    public void otherAttr() {
        // 总页数
        this.totalPages = this.totalElements % this.size > 0 ? this.totalElements / this.size + 1 : this.totalElements / this.size;

    }

    public Integer getNumber() {
        return number;
    }

    public Integer getSize() {
        return size;
    }

    public Integer getTotalPages() {
        return totalPages;
    }

    public List<T> getContent() {
        return content;
    }

    @Override
    public String toString() {
        return "Pager [number=" + number + ", size=" + size
                + ", totalPages=" + totalPages + ", totalElements=" + totalElements
                + ", content=" + content + "]";
    }

}
